/**
 * 知识赛相关的接口替换成了新的接口，参数、返回结构均有变化，在这里统一处理，页面就可以保证最小改动了
 */

import axios from '@/libs/axios1'
import { responesAssemble, requestAssemble, noFilter } from '@/libs/utils'
let jsonHeaders = { 'content-type': 'application/json;charset=UTF-8' }
//保存试题到知识赛
export const saveQuestionToKnowLedge = data => {
  return axios({
    url: `/compete/kePaper`,
    method: 'post',
    data,
    headers: jsonHeaders
  })
}
//删除知识赛在的所有题目
export const deleteAllKnowLedgeQuestion = id => {
  return axios({
    url: `/compete/kePaper/${id}`,
    method: 'delete',
    headers: jsonHeaders
  })
}
//删除知识赛中的题目
export const deleteSingleKnowLedgeQuestion = ids => {
  return axios({
    url:`/compete/kePaper`,
    method: 'delete',
    data:ids,
    headers: jsonHeaders
  })
}
//查询知识赛所有题目
export const queryAllKnowLedgeQuestion = (id,obj) => {
  let params = {
    pageIndex: obj.page,
    pageSize: obj.rows
  }
  return new Promise(resolve => {
    axios({
      url: `/compete/kePaper/${id}`,
      method: 'get',
      params,
      headers: jsonHeaders
    }).then(res => {
      let { code, data, message } = res
      let total = data.total || 0
      let totalScore = data.totalScore || 0
      resolve({
        code: code,
        message: message,
        attribute: {
          totalPage: Math.ceil(total / params.pageSize),
          totalScore: totalScore,
          dataList: data.items,
          totalCount: total,
          currentPage: params.pageIndex
        }
      })
    })
  });
}
//查询竞赛中的用户
export const getSenceUserList = obj => {
  let params = {
    pageIndex: obj.page,
    pageSize: obj.rows,
    order: obj.order,
    sortBy: obj.sortBy,
    id: obj.id,
    sceneId: obj.refId,
    userId: obj.userId,
    teamName: obj.teamName,
    createTime: obj.createTime,
    sceneType: obj.sceneType,
    userName: obj.userName
  }
  return new Promise(resolve => {
    axios({
      url: `/compete/sceneUser/users/page`,
      method: 'get',
      params,
      headers: jsonHeaders
    }).then(res => {
      let { code, data, message } = res
      let total = data.total || 0
      let items = (data.items || []).map(item => {
        return {
          teamName: item.teamName,
          refGroupId: item.teamId,
          userName: item.userName,
          userId: item.userId,
          createTime: item.createTime,
          sceneType: item.sceneType,
          id: item.id,
          creator: item.creatorName
        }
      })
      resolve({
        code: code,
        message: message,
        attribute: {
          totalPage: Math.ceil(total / params.pageSize),
          dataList: items,
          totalCount: total,
          currentPage: params.pageIndex
        }
      })
    })
  });
}
//删除竞赛下的用户
export const delSenceUsers = (id,userIds) => {
  return axios({
    url: `/compete/sceneUser/deleteByUserIds/${id}`,
    method: 'delete',
    data:userIds,
    headers: jsonHeaders
  })
}
//查询用户答题详情
export const queryAllKnowLedgeQuestionByUser = (id,userid,params) => {
  params = requestAssemble(params)
  return new Promise(resolve => {
    axios({
      url: `/compete/keStatistics/detail/${id}/${userid}`,
      method: 'get',
      params,
      headers: jsonHeaders
    }).then(res => {
      res.data && res.data.items && res.data.items.forEach(item => {
        if ([1, 2].includes(item.modeCode)) {
          item.keAnswerTOS && item.keAnswerTOS.forEach((subItem, index) => {
            if(subItem.description.indexOf('】') == 2 || subItem.description.indexOf(']') == 2) {
              subItem.description = subItem.description.slice(3);
            }
            subItem.description = `[${noFilter(index)}]${subItem.description}`
          })
        }
      })
      resolve(responesAssemble(res, params))
    })
  });
}
//统计接口答题排名情况接口
export const staticRank = (id,params) => {
  params = requestAssemble(params)
  return new Promise(resolve => {
    axios({
      url: `/compete/keStatistics/rank/${id}`,
      method: 'get',
      params,
      headers: jsonHeaders
    }).then(res => {
      let ret = responesAssemble(res, params)
      resolve(ret)
    })
  });
}

// 获取人数和解题总数
export const getEvePel = (id,params) => {
    return axios({
        url: `/compete/getKnowledgeLiveInfo/${id}?submitTime=${params.submitTime}`,
        method: 'post',
        data:{},
        headers: jsonHeaders
    })
}
